#!bin/bash
#this script just run onece
CORENUM1=4
CORENUM=$((${CORENUM1} - 1))
SETUSAGE=0.7
while :
do
	NOWFREQ=`top -b -n1 | grep -v "Cpu(s)" | grep -v top | awk  '{print $7}' | awk '{sum+=$1} END {print sum}'`
	MAXUSAGE="$(awk 'BEGIN{print "'$CORENUM1'" * 100}')"
	CURFREQ=`echo "$MAXUSAGE $NOWFREQ" | awk '{printf("%.2f",$2/$1)}'`
	if [[ ${CURFREQ} ]] && [[ ${CURFREQ} > ${SETUSAGE} ]];then
		for i in $(seq 0 $CORENUM); do
			GETMODE="cat /sys/devices/system/cpu/cpu"$i"/cpufreq/scaling_governor"
			NOWMODE=`$GETMODE`
				if [ "$NOWMODE"x = powersavex ];then
					/opt/bin/cpufreq-set -c $i -g performance 
					sleep 0.2
				fi
			#/opt/bin/cpufreq-set -c $i -g powersave
		done
	else
		for i in $(seq 0 $CORENUM); do
			GETMODE="cat /sys/devices/system/cpu/cpu"$i"/cpufreq/scaling_governor"
			NOWMODE=`$GETMODE`
				if [ "$NOWMODE"x = performancex ];then
					/opt/bin/cpufreq-set -c $i -g powersave
					sleep 0.2
				fi
		done
	fi
#	/opt/bin/cpufreq-set
	sleep 5
done